<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\admin\BaseController;
use think\Exception;
use app\common\model\content\Tag as TagModel;
use think\facade\Log;

class Tag extends BaseController
{
    //是否关联user模型( 用于权限规则验证 )
    public $relateUser = true;

    //中间件
    protected $middleware = [
        'method:ajax' => ['only'=>['list','save','update','delete','deleteBatch']],
    ];


    /**
     *
     * #获取首页列表数据
     * @return \think\response\Json
     */
    public function list(){

        try{
            //获取列表数据
            $data = TagModel::getListData($this->ruleWhere());


        }catch (\Exception $e){
            //系统错误
            //Log::error($e->getMessage());
            return json([
                'code' => 1,
                'msg' => $e->getMessage(),
            ]);
        }

        //成功返回
        return json([
            'code'  => 0,
            'msg'=> '查询列表成功',
            'count' => $data['count'],
            'data'  => $data['data']
        ]);

    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        return view('tag/index');
    }


    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        return view('tag/create');
    }



    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save()
    {
        $model = $this->getModel(TagModel::class);
        return $this->store($model, '添加标签成功','添加标签失败');
    }


    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit(int $id)
    {
        return view('tag/edit',[
            'model' => $this->getModel(TagModel::class,$id)
        ]);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(int $id)
    {
        $model = $this->getModel(TagModel::class,$id);
        return $this->store($model, '修改标签成功','修改标签失败');
    }

    private function store(TagModel $model, $sucMsg, $errMsg){
        try{
            $data = request()->only(['name']);
            validate(['name'=>'require|max:15'])->check($data);
            if ( !$model->save($data) )throw new Exception($errMsg);

        }catch (\Exception $e){
            //Log::error($e->getMessage());
            return json([
                'code' => 1,
                'msg' => $e->getMessage()
            ]);
        }
        return json([
            'code' => 0,
            'msg' => $sucMsg
        ]);
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete(int $id)
    {
        try{
            if ($this->getModel(TagModel::class,$id)->delete() === false){
                throw new Exception('删除标签失败.');
            }

        }catch (\Exception $e){
            //Log::error($e->getMessage());
            return json(['code'=>1, 'msg'=>$e->getMessage()]);
        }
        return json(['code'=>0,'msg'=>'删除标签成功.']);

    }

    /**
     * #批量删除操作
     * @return \think\response\Json
     */
    public function deleteBatch(){
        try{
            $ids = explode(',', trim(request()->param('ids'),','));
            if ($this->batchRule(TagModel::class, (array) $ids) &&
                TagModel::destroy($ids) === false )
            {
                throw new Exception('批量删除标签失败.');
            }

        }catch (\Exception $e){
            //Log::error($e->getMessage());
            return json(['code'=>1, 'msg'=>$e->getMessage()]);
        }
        return json(['code'=>0,'msg'=>'批量删除标签成功.']);
    }

}
